/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.room.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.examination.entity.Texamination;
import com.jeesite.modules.zx.examination.room.entity.TexaminationRoom;
import com.jeesite.modules.zx.examination.room.service.TexaminationRoomService;
import com.jeesite.modules.zx.examination.service.TexaminationService;

/**
 * 考试考场Controller
 * @author zpp
 * @version 2020-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/room/texaminationRoom")
public class TexaminationRoomController extends BaseController {

	@Autowired
	private TexaminationRoomService texaminationRoomService;
	@Autowired
	private TexaminationService texaminationService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationRoom get(String id, boolean isNewRecord) {
		return texaminationRoomService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationRoom texaminationRoom, Model model) {
		model.addAttribute("texaminationRoom", texaminationRoom);
		return "modules/zx/examination/room/texaminationRoomList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TexaminationRoom> listData(TexaminationRoom texaminationRoom, HttpServletRequest request, HttpServletResponse response) {
		texaminationRoom.setPage(new Page<>(request, response));
		Page<TexaminationRoom> page = texaminationRoomService.findPage(texaminationRoom);
		for(TexaminationRoom room:page.getList()){
			String examId=room.getExamId();
			if(StringUtils.isNotBlank(examId)){
				Texamination texamination = texaminationService.get(examId);
				room.setExamName(texamination.getObjName());
			}
		}
		
		return page;
	}
	@RequestMapping(value = "findList")
	@ResponseBody
	public List<TexaminationRoom> findList(TexaminationRoom texaminationRoom, HttpServletRequest request, HttpServletResponse response) {
		String examId=texaminationRoom.getExamId();
		if(StringUtils.isBlank(examId)){
			return new ArrayList<TexaminationRoom>();
		}
		List<TexaminationRoom> findList = texaminationRoomService.findList(texaminationRoom);
		return findList;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:view")
	@RequestMapping(value = "form")
	public String form(TexaminationRoom texaminationRoom, Model model) {
		model.addAttribute("texaminationRoom", texaminationRoom);
		return "modules/zx/examination/room/texaminationRoomForm";
	}

	/**
	 * 保存考试考场
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationRoom texaminationRoom) {
		texaminationRoomService.save(texaminationRoom);
		return renderResult(Global.TRUE, text("保存考试考场成功！"));
	}
	
	/**
	 * 停用考试考场
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationRoom texaminationRoom) {
		texaminationRoom.setStatus(TexaminationRoom.STATUS_DISABLE);
		texaminationRoomService.updateStatus(texaminationRoom);
		return renderResult(Global.TRUE, text("停用考试考场成功"));
	}
	
	/**
	 * 启用考试考场
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationRoom texaminationRoom) {
		texaminationRoom.setStatus(TexaminationRoom.STATUS_NORMAL);
		texaminationRoomService.updateStatus(texaminationRoom);
		return renderResult(Global.TRUE, text("启用考试考场成功"));
	}
	
	/**
	 * 删除考试考场
	 */
	@RequiresPermissions("zx:examination:room:texaminationRoom:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationRoom texaminationRoom) {
		texaminationRoomService.delete(texaminationRoom);
		return renderResult(Global.TRUE, text("删除考试考场成功！"));
	}
	
}